//
//  NEBaseTeamSearchFileCell.swift
//  NETeamUIKit
//
//  Created by Laura on 2024/7/31.
//


import NEChatUIKit
import NECommonKit
import NIMSDK
import UIKit

@objcMembers
open class NEBaseTeamSearchFileCell: UITableViewCell {

  /// 用户昵称
  public lazy var nameLabel: UILabel = {
    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    label.textColor = .normalTeamTitleColor
    label.font = NEConstant.defaultTextFont(16)
    label.textAlignment = .left
    label.accessibilityIdentifier = "id.name"
    return label
  }()

    /// 文件
    public lazy var fileImageView: UIImageView = {
      let headView = NEUserHeaderView(frame: .zero)
      headView.titleLabel.textColor = .white
      headView.titleLabel.font = NEConstant.defaultTextFont(14)
      headView.translatesAutoresizingMaskIntoConstraints = false
      headView.layer.cornerRadius = 6
      headView.clipsToBounds = true
      return headView
    }()
    
  /// 消息内容
  public lazy var fileNameLabel: UILabel = {
    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    label.textColor = .normalTeamTitleColor
    label.font = UIFont.systemFont(ofSize: 14, weight: .medium)
    label.textAlignment = .left
    label.accessibilityIdentifier = "id.message"
    return label
  }()

    public lazy var sizeLabel: UILabel = {
      let label = UILabel()
      label.translatesAutoresizingMaskIntoConstraints = false
      label.textColor = .normalTeamTipTitleColor
      label.font = NEConstant.defaultTextFont(13.0)
      label.textAlignment = .left
      label.accessibilityIdentifier = "id.displaySize"
      return label
    }()

  /// 消息时间
  public lazy var timeLabel: UILabel = {
    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    label.textColor = .normalTeamSubTitleColor
    label.font = NEConstant.defaultTextFont(13)
    label.textAlignment = .right
    label.accessibilityIdentifier = "id.time"
    return label
  }()

    /// 分隔线
    public lazy var bottomLine: UIView = {
      let view = UIView()
      view.translatesAutoresizingMaskIntoConstraints = false
      view.backgroundColor = UIColor(hexString: "0xDBE0E8")
      return view
    }()
    
  override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    setupSubviews()
  }

  public required init?(coder: NSCoder) {
    super.init(coder: coder)
  }

  open func setupSubviews() {
      selectionStyle = .none
      contentView.addSubview(nameLabel)
      contentView.addSubview(fileImageView)
      contentView.addSubview(fileNameLabel)
      contentView.addSubview(sizeLabel)
      contentView.addSubview(timeLabel)
      contentView.addSubview(bottomLine)
  }

  open func configData(message: HistoryMessageModel) {
      nameLabel.text = message.fullName
      
      if let createTime = message.imMessage?.createTime {
          let messageTime = Date(timeIntervalSince1970: createTime)
          let dateFormatter = DateFormatter()
          dateFormatter.dateFormat = "yyyy/MM/dd"
          let dateString = dateFormatter.string(from: messageTime)
          timeLabel.text = dateString
      } else {
          timeLabel.text = ""
      }      
      
      if let fileObject = message.imMessage?.attachment as? V2NIMMessageFileAttachment {
          var imageName = "file_unknown"
          let ext = (fileObject.name as NSString).pathExtension.lowercased()

          if file_doc_support.contains(ext) {
              imageName = "file_doc"
          } else if file_xls_support.contains(ext) {
              imageName = "file_xls"
          } else if file_img_support.contains(ext) {
              imageName = "file_img"
          } else if file_ppt_support.contains(ext) {
              imageName = "file_ppt"
          } else if file_txt_support.contains(ext) {
              imageName = "file_txt"
          } else if file_audio_support.contains(ext) {
              imageName = "file_audio"
          } else if file_vedio_support.contains(ext) {
              imageName = "file_vedio"
          } else if file_zip_support.contains(ext) {
              imageName = "file_zip"
          } else if file_pdf_support.contains(ext) {
              imageName = "file_pdf"
          } else if file_html_support.contains(ext) {
              imageName = "file_html"
          } else if ["key", "keynote"].contains(ext) {
              imageName = "file_keynote"
          }
          
          fileImageView.image = coreLoader.loadImage(imageName)
          fileNameLabel.text = fileObject.name
          
          let size_B = Double(fileObject.size)
          var size_str = String(format: "%.1f B", size_B)
          if size_B > 1e3 {
            let size_KB = size_B / 1e3
            size_str = String(format: "%.1f KB", size_KB)
            if size_KB > 1e3 {
              let size_MB = size_KB / 1e3
              size_str = String(format: "%.1f MB", size_MB)
              if size_MB > 1e3 {
                let size_GB = size_KB / 1e6
                size_str = String(format: "%.1f GB", size_GB)
              }
            }
          }
          sizeLabel.text = size_str
      }
      
      
  }
    
    public func jsonStringToDictionary(jsonString: String) -> [String: Any]? {
        guard let jsonData = jsonString.data(using: .utf8) else { return nil }
        
        do {
            let jsonDict = try JSONSerialization.jsonObject(with: jsonData, options: []) as? [String: Any]
            return jsonDict
        } catch {
            print("Error parsing JSON: \(error)")
            return nil
        }
    }
}
